﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ClockIn.Controls
{
    class MyDataGridView : DataGridView
    {

        public MyDataGridView()
        {
        }

        bool m_ChangingSelection = false;
        protected override void OnSelectionChanged(EventArgs e)
        {
            if (!m_ChangingSelection &&
                this.SelectedRows.Count > 0)
            {
                DataGridViewRow currentRow = this.CurrentRow;

                bool useLatestRow = false;
                DataGridViewRow latestRow = this.SelectedRows[0];
                int lastRowIndex = latestRow.Index;
                for (int i = 1; i < this.SelectedRows.Count; i++)
                {
                    int thisRowIndex = this.SelectedRows[i].Index;
                    if (Math.Abs(thisRowIndex - lastRowIndex) != 1)
                    {
                        useLatestRow = true;
                        break;
                    }
                    lastRowIndex = thisRowIndex;
                }

                if (useLatestRow)
                {
                    m_ChangingSelection = true;
                    ClearSelection();
                    currentRow.Selected = true;
                    m_ChangingSelection = false;
                }
            }

            base.OnSelectionChanged(e);
        }
    }
}
